package com.fancon.android.hotgirlcollection;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import com.fancon.android.activity.FanconBaseActivity;
import com.fancon.android.multithread.RequestUI;
import com.fancon.android.rpc.XChannelApiService;
import com.fancon.android.tech.pulltorefresh.PullToRefreshBase.OnLoadmoreListener;
import com.fancon.android.tech.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import com.fancon.android.tech.pulltorefresh.PullToRefreshListView;
import com.fancon.android.ui.widget.FanconProgressBar;
import com.fancon.android.xchannel.entity.Photo;

public class XChannelActivity extends FanconBaseActivity {
    /** Called when the activity is first created. */
	private Boolean isRefresh = true;
	private int index = 0;
	private ArrayList<Photo> listPhotos = new ArrayList<Photo>();
	private PullToRefreshListView mPullRefreshGridView;
	private ListView mGrid;
	private XChannelAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mPullRefreshGridView = (PullToRefreshListView) findViewById(R.id.pull_list);
        mGrid = mPullRefreshGridView.getRefreshableView();
		mGrid.setDivider(null);
		mGrid.setCacheColorHint(0);
		mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				refresh();
			}
		});
		mPullRefreshGridView.setOnLoadmoreListener(new OnLoadmoreListener() {

			@Override
			public void onLoadmore() {
				// TODO Auto-generated method stub
				loadMore();
			}
		});
        load();
    }
    public void refresh(){
    	isRefresh = true;
    	index =0;
    	//listPhotos.clear();
    	load();
    }
    public void loadMore(){
    	index++;
    	load();
    }
    public void load(){
		UserPhotoRequest photoReq = new UserPhotoRequest("photo_rq",this);
    	getGlobalState().getRequestQueue().addRequest(photoReq);
    }
    private void loadDataView(){
		if (listPhotos.size() > 0) {
			if(listPhotos.size() %100 ==0){
				mPullRefreshGridView.setLoadMoreVisible(true);
			}
		} else {
			mPullRefreshGridView.setLoadMoreVisible(false);
		}
    	adapter = new XChannelAdapter(this, listPhotos);
		mGrid.setAdapter(adapter);
    }
	class UserPhotoRequest extends RequestUI {
		private Activity context;
		private FanconProgressBar progress;

		public UserPhotoRequest(Object key, Activity activity) {
			super(key, activity);
			// TODO Auto-generated constructor stub
			context = activity;
			if (isRefresh) {
				progress = FanconProgressBar.show(context, context
								.getString(R.string.common_loading));
			}
		}

		@Override
		public void execute() throws Exception {
			XChannelApiService api = new XChannelApiService(context);
			ArrayList<Photo> tmp = api.getPhotos(index);
			if(listPhotos.size() >0 && !isRefresh){
				listPhotos.addAll(tmp);
			}else{
				listPhotos = tmp;
			}
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			if(isRefresh){
				isRefresh = false;
				mPullRefreshGridView.onRefreshComplete();
			}
			if(progress != null){
				progress.dismiss();
			}
			loadDataView();
		}

	}
}